import 'package:xvideos/hj_model/actress_info.dart';
import 'package:xvideos/hj_model/list_base_model.dart';
import 'package:xvideos/hj_model/message/search_user_obj.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_utils/global.dart';
import 'package:xvideos/hj_utils/http_manager.dart';

import '../../alert_business/video/recommend_video_res.dart';
import '../../config/config.dart';
import '../../hj_model/game_linkage/game_linkage_model.dart';
import '../../hj_model/home/collection_resp.dart';
import '../../hj_model/home/home_video_library.dart';
import '../../hj_model/home/module_detail_model.dart';
import '../../hj_model/home/video_list_resp.dart';
import '../../hj_model/rank_info_model.dart';
import '../../hj_model/splash/domain_source_model.dart';
import '../../hj_model/splash/watch_count_model.dart';
import '../../hj_model/user/aw_info_resp.dart';

class VidService {
  ///获取短视频标签
  static Future<VideoListResp?> shortFindTabList(
    int pageNumber,
    int pageSize, {
    String? tagID, //video 动画（默认不填显示这个） image 漫画 text 小说
    int? type, //1-最热  2-最新  4 热卖 5 指定tagx
  }) async {
    final param = <String, dynamic>{
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'tagID': tagID,
      'type': type,
    };
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByGET(
      '/recommend/vid/list/discover',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  /// 根据id获取观看次数
  /// [vid] 视频vid 可null
  static Future<WatchCount?> fetchVideoWatchCount({String? vid}) async {
    final param = <String, dynamic>{'vid': vid};
    param.removeWhere((key, value) => value == null);
    final result =
        await httpManager.fetchResponseByGET('/vid/user/count', param: param, jsonTransformation: (json) => WatchCount.fromJson(json));
    if (result.isSuccess) return result.data;
    return null;
  }

  /// 发送观看视频记录
  static Future<bool> sendVideoRecord(
    String videoID, {
    int? playWay,
    int? longer,
    int? progress,
    int? via,
    String? tagID,
  }) async {
    final param = {
      'videoID': videoID,
      'playWay': playWay,
      'longer': longer,
      'progress': progress,
      'via': via,
      'tagID': tagID,
    };
    final result = await httpManager.fetchResponseByPOST('/vid/play', param: param);
    return result.isSuccess;
  }

  /// 用户上传视频发帖接口
  /// [actor] 演员名字
  /// [bountyPoint] 求车牌悬赏积分
  /// [coins] 观看金币
  /// [content] 发帖内容
  /// [cover] 视频封面
  /// [coverThumb] 封面缩略图
  /// [filename] 上传的文件名 一般视频用
  /// [freeTime] 免费观看时长
  /// [isActivity] 是否为参赛作品
  /// [md5] 文件摘要上传视频需要
  /// [mimeType] 影片类型
  /// [newsType] 上传类型 SP:短视频,MOVIE:影视,COVER:图组,POST:帖子,CAR_NO:求车牌
  /// [playTime] 视频时长
  /// [ratio] 宽高比
  /// [relatedSid] 关联番号
  /// [resolution] 分辨率
  /// [seedLink] 磁力链接
  /// [seriesCover] 图集
  /// [size] 文件大小
  /// [sourceID] 上传视频成功后 返回的ID 为SP时必传
  /// [sourceURL]  资源url 为SP,MOVIE时 必传
  /// [tags] 视频标签数组
  /// [title] 发帖的标题
  /// [uid] 用户id
  static Future<bool> submitVideo(
      {String? actor,
      int? bountyPoint,
      int? coins,
      String? content,
      String? cover,
      String? coverThumb,
      String? filename,
      int? freeTime,
      bool? isActivity,
      String? md5,
      String? mimeType,
      String? newsType,
      int? playTime,
      double? ratio,
      String? relatedSid,
      String? resolution,
      List<String>? seriesCover,
      int? size,
      String? sourceID,
      String? sourceURL,
      List<String>? tags,
      String? title,
      int? uid,
      String? via,
      String? seedLink}) async {
    final param = {
      'title': title,
      'newsType': newsType,
      'tags': tags,
      'playTime': playTime,
      'cover': cover,
      'coverThumb': coverThumb,
      'seriesCover': seriesCover,
      'via': via,
      'coins': coins,
      'size': size,
      'mimeType': mimeType,
      'actor': actor,
      'sourceURL': sourceURL,
      'sourceID': sourceID,
      'filename': filename,
      'resolution': resolution,
      'ratio': ratio,
      'md5': md5,
      'freeTime': freeTime,
      'isActivity': isActivity,
      'content': content,
      'seedLink': seedLink,
      'relatedSid': relatedSid,
      'bountyPoint': bountyPoint,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByPOST('/vid/submit', param: param);
    if (!result.isSuccess) {
      showToast(result.toast);
    }
    return result.isSuccess;
  }

  ///获取模块详情
  /// [moduleSort] 默认视频排序 1、最新，2、最热，3、最多播放量或者推荐，4、十分钟以上视频，5、精华，6、视频
  /// [showType]0 默认为hj
  static Future<ModuleDetailModel?> getModuleDetail(
    String? subModuleID, {
        int? moduleSort,
        int pageNumber = 1,
        int pageSize = 10,
        int? showType,
        String? tagId
  }) async {
    final param = <String, dynamic>{
      'pageSize': pageSize,
      'pageNumber': pageNumber,
      'moduleSort': moduleSort,
      'showType': showType,
      'tagId': tagId,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/module/$subModuleID',
      param: param,
      jsonTransformation: (json) => ModuleDetailModel.fromJson(json),
    );
    return result.data;
  }

  static Future<dynamic> getRemoteShopAddress() async {
    final _data = <String, dynamic>{};
    final _result = await httpManager.fetchResponseByGET(
      '/ping/store_url',
      param: _data,
    );
    return _result.data;
  }

  ///获取吃瓜热门模块详情
  /// moduleSort 默认视频排序 0、综合 1、最新  2、最多喜欢 3、最多播放量
  static Future<ModuleDetailModel?> getPostHotDetail({
    int? moduleSort,
    int pageNumber = 1,
    int pageSize = 10,
    int? type, // 0 社区， 1套图站热门推荐,  2 游戏，热门推荐
  }) async {
    final param = <String, dynamic>{
      'pageSize': pageSize,
      'pageNumber': pageNumber,
      'moduleSort': moduleSort,
      'type': type,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/module/sns/all',
      param: param,
      jsonTransformation: (json) => ModuleDetailModel.fromJson(json),
    );
    return result.data;
  }

  /// 获取交友模块下的数据
  static Future<ModuleDetailModel?> fetchMakeFriendsData(
    int moduleSort, {
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      'pageSize': pageSize,
      'pageNumber': pageNumber,
      'moduleSort': moduleSort,
    };

    final result = await httpManager.fetchResponseByGET(
      '/vid/blackTopic/all',
      param: param,
      jsonTransformation: (json) => ModuleDetailModel.fromJson(json),
    );
    return result.data;
  }

// 获取视屏详情 番号 番号即是视频id
  static Future<VideoModel?> getVideoDetail(
    String videoID, {
    String? sectionId,
    String? postId,
    String? seedId,
  }) async {
    final param = {"videoID": videoID, "sectionId": sectionId, "id": postId, 'seedId': seedId};
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/info',
      param: param,
      jsonTransformation: (json) => VideoModel.fromJson(json),
    );
    return result.data;
  }

  //根据各种条件获取视频的列表
  static Future<RecommendListRes?> getShortNewestList(int? type, int? pageNumber, int? pageSize) async {
    final param = {"type": type, "pageNumber": pageNumber, "pageSize": pageSize};
    final result =
        await httpManager.fetchResponseByGET('/vid/list', param: param, jsonTransformation: (json) => RecommendListRes.fromJson(json));
    return result.data;
  }

  ///获取推荐视频
  static Future<RecommendListRes?> getRecommendVideoList(int? pageNumber, [int? pageSize = 10]) async {
    final param = {r'pageNumber': pageNumber, r'pageSize': pageSize};
    final result = await httpManager.fetchResponseByGET(
      '/recommend/vid/list',
      param: param,
      jsonTransformation: (json) => RecommendListRes.fromJson(json),
    );
    return result.data;
  }

  static Future<VideoListResp?> fetchSectionVideos(
    String sectionID, {
    String? sortType,
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "sortType": sortType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/section/$sectionID',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  static Future<ListBaseModel<CollectionModel>?> fetchCollection({
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/collection/list',
      param: param,
      jsonTransformation: (json) => ListBaseModel<CollectionModel>.fromJson(json),
    );
    return result.data;
  }

  static Future<VideoListResp?> fetchCollectionVideos({
    String? id,
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      "id": id,
      "pageNumber": pageNumber,
      "pageSize": pageSize,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/collection/vid/list',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  static Future<AwInfoResp?> fetchAwVipInfo() async {
    final param = <String, dynamic>{};
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/product/getAwVip',
      param: param,
      jsonTransformation: (json) => AwInfoResp.fromJson(json),
    );
    return result.data;
  }

  static Future<AwInfoResp?> fetchAwVipCheck() async {
    final param = <String, dynamic>{};
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/modules/check',
      param: param,
    );
    return result.data;
  }

////sortType 0 最新热播; 1 本月最热; 2 上月最热
  static Future<VideoListResp?> fetchVideosLibrary({
    int sortType = 1,
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "sortType": sortType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/long/gold',
      param: param,
      jsonTransformation: (json) => VideoListResp.fromJson(json),
    );
    return result.data;
  }

  static Future<SectionListResp?> fetchSectionAll(
    String mid, {
    int pageNumber = 1,
    int pageSize = 10,
  }) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "mid": mid,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/section/list',
      param: param,
      jsonTransformation: (json) => SectionListResp.fromJson(json),
    );
    return result.data;
  }

  /// 片库搜索视频
  /// 片库搜索视频
  static Future<HomeVideoLibraryResult?> fetchVidLibraryBySearch(
    int pageNumber,
    int pageSize, {
    Keyword? filterMenu,
  }) async {
    final param = <String, dynamic>{
      'pageNumber': pageNumber,
      'pageSize': pageSize,
    };
    param.addAll({'keyword': filterMenu?.toJson() ?? {}});
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByPOST(
      '/vid/library/search',
      param: param,
      jsonTransformation: (json) => HomeVideoLibraryResult.fromJson(json),
    );
    return result.data;
  }

  /// 获取福利任务下面的视频
  static Future<List<WareDiscountAreaData>?> fetchFreeareaVideoSource() async {
    final result = await httpManager.fetchResponseByGET('/vid/discount/area',
        param: {}, jsonTransformation: (json) => (json['List'] as List?)?.map((e) => WareDiscountAreaData.fromJson(e)).toList());

    return result.data;
  }

  /// 获取免费福利视频数据
  /// [sortType] 0 = 福利任务下面 1 最新 2最热
  static Future<ListBaseModel<VideoModel>> fetchFreeareaVideoSourceList(String id,
      {int page = 1, int size = 10, required int sortType}) async {
    final param = {
      'pageNumber': page,
      'pageSize': size,
      'discountId': id,
      'sortType': sortType,
    };
    final result = await httpManager.fetchResponseByGET('/vid/discount/list',
        param: param, jsonTransformation: (json) => ListBaseModel<VideoModel>.fromJson(json));
    return result.data;
  }

  /// 排行榜
  static Future<RankInfoModel?> topInfo(String vid) async {
    final param = <String, dynamic>{
      'vid': vid,
    };
    final result = await httpManager.fetchResponseByGET('/top/vid/topInfo', param: param);
    if (result.isSuccess) {
      return RankInfoModel.fromJson(result.data);
    }
    return null;
  }

  static Future<List<ActressInfo>> fetchMoreModuleSectionList(String moduleId, {int page = 1, int size = 10}) async {
    final param = <String, dynamic>{
      // 'pageNumber': page,
      // 'pageSize': size,
      'mid': moduleId,

      'pageSize': size,
      'pageNumber': page,
    };

    final result = await httpManager.fetchResponseByGET('/modules/section/list',
        param: param, jsonTransformation: (json) => (json['list'] as List?)?.map((e) => ActressInfo.fromJson(e)).toList());
    return result.data;
  }

  static Future<List<VideoModel>> fetchCommunityHotWorks(
    String loadType, {
    int page = 1,
    int size = 20,
    String sort = 'new',
  }) async {
    final param = <String, dynamic>{
      // 'pageNumber': page,
      // 'pageSize': size,
      'newsType': loadType,
      'sortType': sort,
      'pageSize': size,
      'pageNumber': page,
    };

    final result = await httpManager.fetchResponseByGET('/vid/community/hot/list',
        param: param, jsonTransformation: (json) => (json['list'] as List?)?.map((e) => VideoModel.fromJson(e)).toList());
    return result.data;
  }

  static Future<AwVipInfoResp> getHqVipInfo() async {
    final param = <String, dynamic>{};
    final result = await httpManager.fetchResponseByGET(
      '/product/getHqVip',
      param: param,
      jsonTransformation: (json) => AwVipInfoResp.fromJson(json),
    );
    return result.data;
  }

  static Future<GameLinkageModel> gameLinkageConfig() async {
    final _data = {};
    _data.removeWhere((k, v) => v == null);
    final _result = await httpManager.fetchResponseByGET('/advance_config/list');
    final value = _result.data;
    return GameLinkageModel.fromJson(value);
  }

  /// 获取跑马灯数据
  static Future<List<MarqueeModel>?> fetchAnnounce(int type) async {
    final param = <String, dynamic>{"type": type};
    final result = await httpManager.fetchResponseByGET('/mine/announce/list', param: param);

    final List<MarqueeModel> list = [];
    if (result.isSuccess) {
      List? values = result.data as List?;
      list.addAll(values?.map((e) => MarqueeModel.fromMap(e)).toList() ?? []);
    }
    Config.allMarquee = list;
    return list;
  }

  /// 获取跑马灯数据
  static Future<ListBaseModel<HotBloggerEntity>?> fetchUpList(int pageNumber, int pageSize) async {
    final param = <String, dynamic>{"pageNumber": pageNumber, "pageSize": pageSize};
    final result = await httpManager.fetchResponseByGET(
      '/mine/upList',
      param: param,
      jsonTransformation: (json) => ListBaseModel<HotBloggerEntity>.fromJson(json),
    );
    return result.data;
  }

  //社区热门推荐
  static Future<ModuleDetailModel?> communityRcommend(
    int pageNumber, {
    int? pageSize = 10,
    String? newsType, //PIC:套图站热门推荐 SEED_LINK:黄游热门推荐
    int? sortType = 1, //排序:最新:1 最多点赞:2 最多观看:3 最多收藏:7 购买次数:8
  }) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "newsType": newsType,
      "sortType": sortType,
    };
    final result = await httpManager.fetchResponseByGET(
      '/vid/community/recommend',
      param: param,
      jsonTransformation: (json) => ModuleDetailModel.fromJson(json),
    );
    return result.data;
  }

  //0 最多观看 1 最新上架 3 最多收藏
  static Future<AllSection?> getGuessLike(
    int pageNumber,
    int pageSize,
    String sectionID,
    int? sortType,
  ) async {
    final param = <String, dynamic>{
      "pageNumber": pageNumber,
      "pageSize": pageSize,
      "type": sortType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET(
      '/vid/module/all/' + sectionID,
      param: param,
      jsonTransformation: (json) => AllSection.fromJson(json),
    );
    return result.data;
  }

  /// 获取片库筛选条件
  static Future<HomeVideoLibrary> fetchVideoLibrary() async {
    // final param = <String, dynamic>{'libraryType': libraryType};
    final param = <String, dynamic>{};
    final result =
        await httpManager.fetchResponseByGET('/vid/library', param: param, jsonTransformation: (json) => HomeVideoLibrary.fromJson(json));
    return result.data;
  }
}
